﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace Assignment6
{
    public class MyDbContext:DbContext
    {
        public DbSet<School> schools { get; set; }
        public DbSet<Class> classes { get; set; }
        public DbSet<Student> students { get; set; }
        public DbSet<Logger> loggers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlite("Data Source=Mydata.db");
            }
            base.OnConfiguring(optionsBuilder);
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Class>()
              .HasKey(p => new { p.SchoolId, p.ClassId });
            modelBuilder.Entity<Student>()
              .HasKey(p => new { p.SchoolId, p.StudentId });
            base.OnModelCreating(modelBuilder);
        }
        // 日志记录，需要重写SaveChanges函数
        public override int SaveChanges()
        {
            try
            {
                int changes = base.SaveChanges();
                LogChanges();
                changes = base.SaveChanges();
                return changes;
            }
            catch(Exception ex) 
            {
                Console.WriteLine("Error in SaveChanges: " + ex.Message);
                throw; // 可以选择抛出异常或者不抛出，具体情况而定
            }
        }

        private void LogChanges()
        {
            var addedEntities = ChangeTracker.Entries()
                .Where(e => e.State == EntityState.Added)
                .Select(e => e.Entity);

            var modifiedEntities = ChangeTracker.Entries()
                .Where(e => e.State == EntityState.Modified)
                .Select(e => e.Entity);

            var deletedEntities = ChangeTracker.Entries()
                .Where(e => e.State == EntityState.Deleted)
                .Select(e => e.Entity);

            foreach (var entity in addedEntities)
            {
                // 在这里记录新增操作日志
                // 使用日志库记录日志到文件或数据库
                string uniqueId = Guid.NewGuid().ToString();
                DateTime curTime = DateTime.Now;
                Logger curLog = new Logger()
                {
                    LoggerId = uniqueId,
                    OperationTime = curTime,
                    OperationType = "增加数据",
                    // OperationData = entity
                };
                loggers.Add(curLog);
            }
            foreach (var entity in modifiedEntities)
            {
                // 在这里记录修改操作日志
                // 使用日志库记录日志到文件或数据库
                string uniqueId = Guid.NewGuid().ToString();
                DateTime curTime = DateTime.Now;
                Logger curLog = new Logger()
                {
                    LoggerId = uniqueId,
                    OperationTime = curTime,
                    OperationType = "修改数据",
                    // OperationData = entity
                };
                loggers.Add(curLog);
            }

            foreach (var entity in deletedEntities)
            {
                // 在这里记录删除操作日志
                // 使用日志库记录日志到文件或数据库
                string uniqueId = Guid.NewGuid().ToString();
                DateTime curTime = DateTime.Now;
                Logger curLog = new Logger()
                {
                    LoggerId = uniqueId,
                    OperationTime = curTime,
                    OperationType = "删除数据",
                    // OperationData = entity
                };
                loggers.Add(curLog);
            }
        }
    }
}
